local UIBase = require("view.UIBase")
local TeamGroupView = class("TeamGroupView", UIBase)

local MAX_MEM = 4

function TeamGroupView:ctor(sceneId, callback)
    self._sceneId = sceneId
    self._callback = callback
    local resource = {
        path = "Prefabs/View/TeamGroup",
        name = "TeamGroupView",
        canvas = 2
    }
    TeamGroupView.super.ctor(self, resource)
end

function TeamGroupView:start()
    self.gameObject.name = self.__cname
    local targetId = G_UserData:getTeamGroup():getTargetId()
    local cfg = require("config.monsterhole").get(targetId)
    self._textTitle.text = Lang.get("lang_team_group_target", {name = cfg.name})
    self._btnOK.onClick:AddListener(handler(self, self._onBtnOkClick))
    self._btnPrepare.onClick:AddListener(handler(self, self._onBtnPrepareClick))
    self._btnQuit.onClick:AddListener(handler(self, self._onBtnQuitClick))
    for i = 1, MAX_MEM do
        self["_btnAdd" .. i].onClick:AddListener(handler(self, self._onBtnAddClick))
    end
    self:_updateMembers()
    self._signalId = G_SignalManager:beginCache()
    G_SignalManager:add(SignalConst.EVENT_USER_FLUSH_DATA, handler(self, self._onEventFlushData))
    G_SignalManager:add(SignalConst.EVENT_DEL_TEAM_GROUP, handler(self, self._onEventDelTeam))
    G_SignalManager:add(SignalConst.EVENT_QUIT_TEAM_GROUP, handler(self, self._onEventQuitTeam))
    G_SignalManager:add(SignalConst.EVENT_TEAM_GROUP_NOTIFY, handler(self, self._onEventTeamNorify))
    G_SignalManager:endCache()
end

function TeamGroupView:onDestroy()
    G_SignalManager:removeCache(self._signalId)
end

function TeamGroupView:_onEventFlushData()
    self:close()
end

function TeamGroupView:_onEventDelTeam()
    self:close()
end

function TeamGroupView:_onEventQuitTeam()
    self:close()
end

function TeamGroupView:_onEventTeamNorify()
    self:_updateMembers()
end

function TeamGroupView:_updateMembers()
    local members = G_UserData:getTeamGroup():getMembers()
    for i = 1, MAX_MEM do
        if members[i] then
            self:_updateHero(i, members[i])
        else
            self["_btnAdd" .. i].gameObject:SetActive(true)
            self["_textHero" .. i].gameObject:SetActive(false)
            self["_spine" .. i].gameObject:SetActive(false)
        end
    end
end

function TeamGroupView:_updateHero(index, data)
    self["_btnAdd" .. index].gameObject:SetActive(false)
    self["_textHero" .. index].gameObject:SetActive(true)
    self["_spine" .. index].gameObject:SetActive(true)
    self["_textHero" .. index].text = data.userName
    self["_spine" .. index]:updateByResId("cheyongtaiar")
end

function TeamGroupView:onClose()
    if self._callback then
        self._callback()
    end
end

function TeamGroupView:_onBtnOkClick()
    local myId = G_UserData:getBase():getId()
    local leaderId = G_UserData:getTeamGroup():getLeaderId()
    if myId == leaderId then
        G_UserData:getTeamGroup():c2sTeamGroupEnterScene()
    else
        G_UIManager:showTip(Lang.get("lang_team_groun_enter_fail"))
    end
end

function TeamGroupView:_onBtnPrepareClick()
end

function TeamGroupView:_onBtnQuitClick()
    self:close()
    G_UserData:getTeamGroup():c2sQuitTeam()
end

function TeamGroupView:_onBtnAddClick()
    local nearView = require("view.nearView.NearPlayerView").new(self._sceneId)
end

return TeamGroupView
